package com.fh.shop.api.util;

import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.util.Map;

public class RedisUtil {

    public static void set(String key, String value) {
        Jedis connection = null;
        try {
             connection = RedisPool.getConnection();
             connection.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    public static void hset(String key, String field, String value) {
        Jedis connection = null;
        try {
            connection = RedisPool.getConnection();
            connection.hset(key, field, value);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    public static void hmset(String key, Map<String, String> values) {
        Jedis connection = null;
        try {
            connection = RedisPool.getConnection();
            connection.hmset(key, values);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    public static String hget(String key, String field) {
        Jedis connection = null;
        try {
            connection = RedisPool.getConnection();
            String result = connection.hget(key, field);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    public static String get(String key) {
        Jedis connection = null;
        try {
            connection = RedisPool.getConnection();
            return connection.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

    public static void setEx(String key, String value, int seconds) {
        Jedis connection = null;
        try {
            connection = RedisPool.getConnection();
            connection.setex(key, seconds, value);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if (null != connection) {
                connection.close();
            }
        }
    }



    public static long delete(String k) {
        Jedis connection = null;
        try {
            connection = RedisPool.getConnection();
            return connection.del(k);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if (null != connection) {
                connection.close();
            }
        }
    }

    public static void expire(String key, int seconds) {
        Jedis connection = null;
        try {
            connection = RedisPool.getConnection();
            connection.expire(key, seconds);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if (null != connection) {
                connection.close();
            }
        }
    }


}
